
package com.plian.system.service.fr.authorize.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.plian.Tools.pagedata.PageData;
import com.plian.system.dao.fr.authorize.BudgetAuthorizeDao;
import com.plian.system.entity.fr.authorize.AuthorizeTableConfig;
import com.plian.system.mapper.fr.authorize.AuthorizeTableConfigMapper;
import com.plian.system.service.fr.authorize.IAuthorizeTableConfigService;
import com.plian.system.service.fr.authorize.IAuthorizeTableVersionService;
import com.plian.system.vo.fr.authorize.AuthorizeTableConfigVO;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 预算审定表格配置 前端用 服务实现类
 *
 * @author panqb
 * @since 2021-09-28
 */
@Service
@AllArgsConstructor
public class AuthorizeTableConfigServiceImpl extends ServiceImpl<AuthorizeTableConfigMapper, AuthorizeTableConfig> implements IAuthorizeTableConfigService {

    private IAuthorizeTableVersionService authorizeTableVersionService;

    private BudgetAuthorizeDao budgetAuthorizeDao;

    /**
     * logger属性.
     */
    private static final Logger LOGGER  = LoggerFactory.getLogger(AuthorizeTableConfigServiceImpl.class);


    @Override
    public AuthorizeTableConfigVO getTableConfig(String year) {
        AuthorizeTableConfig reportTableConfig = getOne(new QueryWrapper<AuthorizeTableConfig>().eq("year", year));
        List<PageData> list = new ArrayList<>();
        try {
            PageData pd = new PageData();
            pd.put("version", authorizeTableVersionService.getVersion(year));
            list = budgetAuthorizeDao.listAllOrg(pd);
        }catch (Exception e){
            LOGGER.error("" + e);
        }

        AuthorizeTableConfigVO vo = AuthorizeTableConfigVO.builder()
                .tableConst(JSONArray.parseArray(reportTableConfig.getTableConst()))
                .tableNames(JSON.parseArray(reportTableConfig.getTableNames()))
                .colNames(JSON.parseArray(reportTableConfig.getColNames()))
                .required(JSON.parse(reportTableConfig.getRequired()))
                .rule(JSON.parseArray(reportTableConfig.getRule()))
                .reportOrgList(list)
                .build();

        return vo;
    }

}
